using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using WebExam.BusinessServices;
using WebExam.Core.Common;

namespace WebExam.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private readonly ZGService _zgService;

        public HomeController(ZGService zgService)
        {
            _zgService = zgService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(FormCollection forms) 
        {
            var isLogined = false;

            if(forms["role"]=="zg")
            {
                isLogined = _zgService.IsLogin(forms["account"], forms["pwd"]);
            }
            else if(forms["role"]=="xs")
            {
                
            }

            if(!isLogined)
            {
                return View();
            }

            return RedirectToAction("Index");
        }

        public void ValidateImg()
        {
            var vcode = new ValidateNumber();
            Session["ValidateCode"] = vcode.CreateValidateNumber(5);
            vcode.CreateValidateGraphic(Response, (string)Session["ValidateCode"]);
        }

        [HttpPost]
        public JsonResult ValidateCode(string code)
        {
            return Json(string.Equals(code, (string) Session["ValidateCode"], StringComparison.InvariantCultureIgnoreCase));  
        }
    }
}
